/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.controller.IWifiItemData;
import com.ecarx.systemui.plugin.controller.TcamWifiController;
import com.ecarx.systemui.plugin.controller.WiFiPwdType;
import com.ecarx.systemui.plugin.controller.WifiData;
import com.ecarx.systemui.plugin.controller.WifiItemNothing;
import com.ecarx.systemui.plugin.ui.dialog.InputTextDialog;
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager;

import java.util.List;

public class WifiItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<IWifiItemData> mList;

    public WifiItemAdapter(List<IWifiItemData> list) {
        mList = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new TitleHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wifi_title, parent, false));
        } else if(viewType == 2){
            return new NothingHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wifi_nothing, parent, false));
        }
        return new WifiHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wifi, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        IWifiItemData wifiItemData = mList.get(position);
        if(holder instanceof TitleHolder){
            TitleHolder titleHolder = (TitleHolder) holder;
            titleHolder.updateItemInfo(wifiItemData);
        } else if(holder instanceof WifiHolder) {
            WifiHolder wifiHolder = (WifiHolder)holder;
            wifiHolder.updateWifiInfo(wifiItemData);
            wifiHolder.showLoading(false);
            wifiHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    wifiHolder.showLoading(true);
                    WifiData wifiData = wifiItemData.getWifiData();
                    if(wifiData != null){
                        if (TcamWifiController.INSTANCE.getConnectionWifi() != null && TcamWifiController.INSTANCE.getConnectionWifi().getSsid().equals(wifiData.getSsid())) {
                            TcamWifiController.INSTANCE.disconnect();
                        } else {
                            if (wifiData.isSavePwd()) {
                                TcamWifiController.INSTANCE.connect(wifiData);
                            } else {
                                // 弹出密码框，输入
                                DialogManager.showInputDialog(wifiHolder.itemView.getContext(), true, new InputTextDialog.InputTextListener() {
                                    @Override
                                    public void onInput(@NonNull String text) {
                                        TcamWifiController.INSTANCE.connect(wifiData, text);
                                    }
                                });
                            }
                        }
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getItemType();
    }

    static class WifiHolder extends RecyclerView.ViewHolder {

        TextView tvName;
        ImageView ivLock;
        ImageView ivSignal;
        ProgressBar loading;

        public WifiHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            ivLock = itemView.findViewById(R.id.ivLock);
            ivSignal = itemView.findViewById(R.id.ivSignal);
            loading = itemView.findViewById(R.id.loading);
        }

        public void showLoading(boolean show){
            if (show) {
                loading.setVisibility(View.VISIBLE);
            } else {
                loading.setVisibility(View.GONE);
            }
        }

        public void updateWifiInfo(IWifiItemData wifiItemData) {
            if (wifiItemData == null || wifiItemData.getWifiData() == null) {
                return;
            }
            WifiData wifiData = wifiItemData.getWifiData();
            tvName.setText(wifiData.getSsid());

            ivLock.setVisibility((wifiData.getCapabilities() != WiFiPwdType.ESS) ? View.VISIBLE : View.GONE);

            ivSignal.setBackgroundResource(getSignalResId(wifiData));

            markUsing(itemView.getContext(), !TextUtils.isEmpty(wifiData.getIpAddress()));
        }

        private int getSignalResId(WifiData wifiData){
            int signal = wifiData.getRssi();
            boolean mark = !TextUtils.isEmpty(wifiData.getIpAddress());
            if(signal == 0){
                return mark ? R.drawable.ic_wifi_item_signal_0_make : R.drawable.ic_wifi_item_signal_0;
            } else if(signal == 1){
                return mark ? R.drawable.ic_wifi_item_signal_1_mark : R.drawable.ic_wifi_item_signal_1;
            } else if(signal == 2){
                return mark ? R.drawable.ic_wifi_item_signal_2_mark : R.drawable.ic_wifi_item_signal_2;
            } else if(signal == 3){
                return mark ? R.drawable.ic_wifi_item_signal_3_makr : R.drawable.ic_wifi_item_signal_3;
            }  else if(signal == 4){
                return mark ? R.drawable.ic_wifi_item_signal_4_mark : R.drawable.ic_wifi_item_signal_4;
            }
            return R.drawable.ic_wifi_item_signal_0_make;
        }

        private void markUsing(Context context, boolean isUsing){
            if (isUsing) {
                tvName.setTextColor(context.getColor(R.color.c_7A67FF));
                ivLock.setImageResource(R.drawable.ic_wifi_item_lock_mark);
            } else {
                tvName.setTextColor(context.getColor(R.color.white));
                ivLock.setImageResource(R.drawable.ic_wifi_item_lock);
            }
        }
    }

    static class TitleHolder extends RecyclerView.ViewHolder {

        TextView tvTitle;
        ImageView ivIndicator;

        public TitleHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            ivIndicator = itemView.findViewById(R.id.iv_indicator);
        }

        public void updateItemInfo(IWifiItemData wifiItemData) {
            if (wifiItemData.getItemType() == 0) {
                tvTitle.setText(wifiItemData.isSaved() ? R.string.preferred_network : R.string.other_networks);
                ivIndicator.setVisibility(!wifiItemData.isSaved()? View.VISIBLE : View.GONE);
            }
        }
    }

    static class NothingHolder extends RecyclerView.ViewHolder {

        public NothingHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
